<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Event_m extends CI_Model {
        
        var $config;
        
        function __construct() {
            parent::__construct();
            $this->config = array(
                'start_day' => 'monday',
                'show_next_prev' => true,
                'next_prev_url' => site_url().'events/calendar',
            );
             
            $this->config['template'] = '
                {table_open}<table border="0" cellpadding="0" cellspacing="0" class="calendar">{/table_open}

                {heading_row_start}<tr>{/heading_row_start}

                {heading_previous_cell}<th><a href="{previous_url}" data-transition="flow" data-direction="reverse" data-role="button" data-icon="arrow-l" data-iconpos="notext" class="ui-btn-left">Previous</a></th>{/heading_previous_cell}
                {heading_title_cell}<th colspan="{colspan}" class="month-text">{heading}</th>{/heading_title_cell}
                {heading_next_cell}<th><a href="{next_url}" data-transition="flow" data-role="button" data-icon="arrow-r" data-iconpos="notext" class="ui-btn-right">Next</a></th>{/heading_next_cell}

                {heading_row_end}</tr>{/heading_row_end}

                {week_row_start}<tr>{/week_row_start}
                {week_day_cell}<td class="week-day">{week_day}</td>{/week_day_cell}
                {week_row_end}</tr>{/week_row_end}

                {cal_row_start}<tr class="days">{/cal_row_start}
                {cal_cell_start}<td class="day">{/cal_cell_start}

                {cal_cell_content}
                    <div class="day_num">{day}</div>
                    <a href="#view-event-popup" data-rel="popup" data-transition="slideup">
                        <div class="content">{content}</div>
                    </a>
                {/cal_cell_content}
                {cal_cell_content_today}
                    <div class="day_num highlight">{day}</div>
                    <div class="content">{content}</div>
                {/cal_cell_content_today}

                {cal_cell_no_content}<div class="day_num">{day}</div>{/cal_cell_no_content}
                {cal_cell_no_content_today}<div class="day_num highlight">{day}</div>{/cal_cell_no_content_today}

                {cal_cell_blank}&nbsp;{/cal_cell_blank}

                {cal_cell_end}</td>{/cal_cell_end}
                {cal_row_end}</tr>{/cal_row_end}

                {table_close}</table>{/table_close}
            ';
        }
        
        function generate_calendar($year, $month){
            
            $this->load->library('calendar', $this->config); 
            
            $cal_data = $this->get_calendar_data($year, $month);
            
            return $this->calendar->generate($year, $month, $cal_data);   
        }
        
        function get_calendar_data($year, $month) {
            $q = $this->db->select('event_name, start_date')->from('event')
                    ->like('start_date', "$year-$month", 'after')->get();
            $cal_data = array();
            foreach ($q->result() as $row) {
                $cal_data[substr($row->start_date,8,2)] = $row->event_name; 
            }
            return $cal_data;
        }
        
        function get_events(){
            $this->db->select('*');
            $this->db->from('event');
            $this->db->join('event_type', 'event.type_id=event_type.type_di');
        }
        
        function get_event_types() {
            $q = $this->db->get('event_type');
            if($q->num_rows() > 0){
                return $q->result_array();
            }else{
                return FALSE;
            }
        }
        
        function get_event_type($id) {
            $q = $this->db->where('type_id', $id)->get('event_type');
            if($q->num_rows() > 0){
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function insert_event_type($type_name) {
            $this->db->insert('event_type', array('type_name' => $type_name));
            return $this->db->insert_id();
        }
        
        function update_event_type($type_id, $type_name) {
            $this->db->where('type_id', $type_id)->update('event_type', array('type_name' => $type_name));
        }
        
        function delete_event_type($type_id) {
            for($i=0;$i<sizeof($type_id);$i++){
                $this->db->where('type_id', $type_id[$i])->delete('event_type');
            }
        }
            
        function get_venues() {
            $q = $this->db->get('venue');
            if($q->num_rows() > 0){
                return $q->result_array();
            }else{
                return FALSE;
            }
        }
        
        function get_venue($id) {
            $q = $this->db->where('venue_id', $id)->get('venue');
            if($q->num_rows() > 0){
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function insert_venue($data) {
            $this->db->insert('venue', $data);
            return $this->db->insert_id();
        }
        
        function update_venue($venue_id, $data){
            $this->db->where('venue_id', $venue_id)->update('venue', $data);
            return $this->db->affected_rows();
        }
        
        function delete_venue($venue_id) {
            for($i=0;$i<sizeof($venue_id);$i++){
                $this->db->where('venue_id', $venue_id[$i])->delete('venue');
            }
        }
        
        function insert_event_detail($data) {
            
        }
    }